package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.UUID;



/**
 * @author ：乔帅阳
 * @date ：Created in 2022/1/20 8:01
 * @modified By：
 */

@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Resource
    private RedissonClient redisson;

    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping({"/","/index.html"})
    public String indexPage(Model model, HttpSession session){
        //TODO 查询出所有的1级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
        model.addAttribute("categorys",categoryEntities);
        // classpath:/templates/  默认前缀
        //.html  默认后缀
        return "index";
    }

    @ResponseBody
    @GetMapping(value = "/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatelogJson(){

        Map<String, List<Catelog2Vo>> map = categoryService.getCatelogJson();

        return map;
    }


    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        RLock block = redisson.getLock("my-lock");
        //不指定有效期默认30秒，看门狗规则30除以3每隔10秒自动续期
        block.lock();
        //指定有效期10秒，不会自动续期
     //   block.lock(10,TimeUnit.SECONDS);
        try{
            System.out.println("加锁成功，执行业务。。。"+Thread.currentThread().getId());
            Thread.sleep(3000);
        }catch (Exception e){

        }finally {
            System.out.println("释放锁。。。"+Thread.currentThread().getId());
            block.unlock();
        }
     /*   //有效期10秒
        block.lock(10, TimeUnit.SECONDS);*/
        return "hello";
    }

    @ResponseBody
    @GetMapping("/write")
    public String writeValue(){

        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = lock.writeLock();
        try {
            //1、改数据加写锁，读数据加读锁
             rLock.lock();
             s = UUID.randomUUID().toString();
            Thread.sleep(3000);
            redisTemplate.opsForValue().set("writeValue",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }



    @ResponseBody
    @GetMapping("/read")
    public String readValue(){
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        //加读锁
        RLock rLock = lock.readLock();
        try {
            rLock.lock();
            s = (String) redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }

}
